{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Enter script code\n",
    "\n",
    "\"\"\"REQUIRES MANUAL INPUT \n",
    "0. For Linux: uncomment all (leave those with ##) and remove pyautogui/pyperclip, run on autokey\n",
    "\n",
    "1. Run first block of the notebook\n",
    "\n",
    "2. Search (using Firefox! Need to modify to use other browser) for any case in \n",
    "\n",
    "    https://micourt.courts.michigan.gov/case-search/court/D67/\n",
    "#2015 15T00035 FY worked \n",
    "#2000 20003116 FY failed\n",
    "    to trigger the ToS/Captcha at least once.\n",
    "\n",
    "3. Open the developer console via Ctrl+Shift+I and switch to the console\n",
    "   tab, then close. Cmd+Opt+I in Mac\n",
    "\n",
    "4. Change your downloads folder to\n",
    "\n",
    "    /path/to/D67/3ROAs\n",
    "\n",
    "5. Change ROOT to /path/to/D67\n",
    "\n",
    "6. Run this script with autokey; be sure to use the same browser window\n",
    "\n",
    "7. Repeat as needed (took me a few hours and several restarts; you should\n",
    "   do the captchas when they appear, but otherwise babysitting this script is\n",
    "   pretty passive).\n",
    "\"\"\"\n",
    "#!pip install pyautogui\n",
    "#!pip install pyperclip\n",
    "from pathlib import Path\n",
    "import pyautogui\n",
    "import time\n",
    "import pyperclip\n",
    "\n",
    "cached     = True\n",
    "shortdelay = 0.2\n",
    "longdelay  = 5\n",
    "retrydelay = 30\n",
    "maxretries = 0\n",
    "\n",
    "ROOT = Path('/Users/arkeybarnett/Dropbox/IGNITE/2input_data/D67')\n",
    "OUT  = (ROOT / '3ROAs').resolve()\n",
    "\n",
    "javascript = \"\"\"\n",
    "function download(filename, text) {\n",
    "    var pom = document.createElement('a');\n",
    "    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));\n",
    "    pom.setAttribute('download', filename);\n",
    "    if (document.createEvent) {\n",
    "        var event = document.createEvent('MouseEvents');\n",
    "        event.initEvent('click', true, true);\n",
    "        pom.dispatchEvent(event);\n",
    "    }\n",
    "    else {\n",
    "        pom.click();\n",
    "    }\n",
    "}\n",
    "\n",
    "function clickExpand() {\n",
    "    var buttons = document.getElementsByTagName('button')\n",
    "    for(var i = 0; i < buttons.length; i++) {\n",
    "        if (buttons[i].getAttribute('aria-expanded') == 'false') {\n",
    "            buttons[i].click();\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "def gotopage(queryurl):\n",
    "    time.sleep(3 * shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+l<backspace>\")\n",
    "    ##address bar (MAC) \"Cmd + l\"\n",
    "    pyautogui.hotkey('command', 'l')\n",
    "    time.sleep(shortdelay)\n",
    "\n",
    "    #clipboard.fill_clipboard(queryurl)\n",
    "    pyperclip.copy(queryurl)\n",
    "    time.sleep(shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+v\")\n",
    "    ##(MAC) \"Cmd + v\"\n",
    "    pyautogui.hotkey('command', 'v')\n",
    "    time.sleep(shortdelay)\n",
    "    #keyboard.send_keys(\"<enter>\")\n",
    "      ##(MAC) \"fn + return\"?????\n",
    "    #pyautogui.press('enter')\n",
    "    pyautogui.hotkey('return')\n",
    "    time.sleep(shortdelay)\n",
    "\n",
    "def download(querycase):\n",
    "    time.sleep(shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+<shift>+i\")\n",
    "    ##opening devtools in browser (MAC) \"Cmd + Opt + i\"?????\n",
    "    pyautogui.hotkey('command', 'option', 'i')\n",
    "    time.sleep(5 * shortdelay)\n",
    "\n",
    "    #clipboard.fill_clipboard(javascript)\n",
    "    pyperclip.copy(javascript)\n",
    "    time.sleep(2 * shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+v\")\n",
    "    ##(MAC) \"Cmd + v\"\n",
    "    pyautogui.hotkey('command', 'v')\n",
    "    time.sleep(2 * shortdelay)\n",
    "    #keyboard.send_keys(\"<enter>\")\n",
    "     ##(MAC) \"fn + return\"?????\n",
    "    #pyautogui.press('enter')\n",
    "    pyautogui.hotkey('return')\n",
    "    \n",
    "    #clipboard.fill_clipboard(\"setTimeout(function() { clickExpand(); }, 1000);\")\n",
    "    pyperclip.copy(\"setTimeout(function() { clickExpand(); }, 1000);\")\n",
    "    time.sleep(shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+v<enter>\")\n",
    "     ##(MAC) \"Cmd + v\"\n",
    "    #pyautogui.hotkey('command', 'v', 'enter')\n",
    "    pyautogui.hotkey('command', 'v', 'return')\n",
    "    \n",
    "    time.sleep(1)\n",
    "\n",
    "    #clipboard.fill_clipboard(f'download(\"{querycase}.html\", document.documentElement.innerHTML);')\n",
    "    pyperclip.copy(f'download(\"{querycase}.html\", document.documentElement.innerHTML);')\n",
    "    \n",
    "    time.sleep(shortdelay)\n",
    "    #keyboard.send_keys(\"<ctrl>+v<enter>\")\n",
    "     ##(MAC) \"Cmd + v\"\n",
    "    #pyautogui.hotkey('command', 'v', 'enter')\n",
    "    pyautogui.hotkey('command', 'v', 'return')\n",
    "    time.sleep(1)\n",
    "\n",
    "    #keyboard.send_keys(\"<ctrl>+<shift>+i\")\n",
    "    ##opening devtools in browser (MAC) \"Cmd + Opt + i\"?????\n",
    "    pyautogui.hotkey('command', 'option', 'i')\n",
    "    time.sleep(3 * shortdelay)\n",
    "\n",
    "with open(ROOT / '2case_number/queryurl_OM_SM.txt', 'r') as txt:\n",
    "    queryurls = [x.strip() for x in txt.readlines()]\n",
    "\n",
    "with open(ROOT / '2case_number/querycase_OM_SM.txt', 'r') as txt:\n",
    "    querycases = [x.strip() for x in txt.readlines()]\n",
    "\n",
    "if cached:\n",
    "    queryzip = [(queryurl, querycase) for queryurl, querycase in zip(queryurls, querycases) if not (OUT / (querycase + '.html')).is_file()]\n",
    "else:\n",
    "    queryzip = list(zip(queryurls, querycases))\n",
    "\n",
    "#dialog.info_dialog(\"Please select browser window\", \n",
    "#                   f\"Please select browser window; {len(queryzip)} queries\")\n",
    "\n",
    "pyautogui.alert(f\"Please select browser window; {len(queryzip)} queries\",\n",
    "               \"Please select browser window\")\n",
    "\n",
    "time.sleep(shortdelay)\n",
    "\n",
    "ntries = 0\n",
    "nrequests = 0\n",
    "vpn_switch = 0\n",
    "errstr = \"External dependency responded with\"\n",
    "# errstr = \"Response status code does not indicate success\"\n",
    "for queryurl, querycase in queryzip:\n",
    "    delete   = False\n",
    "    retrynow = False\n",
    "    outfile  = OUT / (querycase + '.html')\n",
    "    if cached and outfile.is_file():\n",
    "        continue\n",
    "\n",
    "    gotopage(queryurl)\n",
    "    nrequests += 1\n",
    "    time.sleep(longdelay)\n",
    "    download(querycase)\n",
    "    if not outfile.is_file():\n",
    "        ntries += 1\n",
    "        time.sleep(retrydelay)\n",
    "    else:\n",
    "        with open(outfile, 'r') as html:\n",
    "            htmlread = html.read()\n",
    "\n",
    "        if 'Error!' in htmlread and errstr in htmlread:\n",
    "            delete  = False  # True?\n",
    "        elif querycase not in htmlread: \n",
    "            retrynow = True\n",
    "            delete   = True\n",
    "            ntries  += 1\n",
    "            time.sleep(retrydelay)\n",
    "\n",
    "        if delete:\n",
    "            outfile.unlink()\n",
    "            delete = False\n",
    "\n",
    "        if retrynow:\n",
    "            download(querycase)\n",
    "            with open(outfile, 'r') as html:\n",
    "                htmlread = html.read()\n",
    "\n",
    "            if querycase not in htmlread: \n",
    "                outfile.unlink()\n",
    "    '''if nrequests % 19 == 0 and nrequests>10:\n",
    "        vpn_switch += 1\n",
    "        \n",
    "        pyautogui.getWindowsWithTitle('ExpressVPN')[0].maximize()\n",
    "        pyautogui.getWindowsWithTitle('ExpressVPN')[0].activate()\n",
    "\n",
    "        pyautogui.press('enter')\n",
    "\n",
    "        time.sleep(1)\n",
    "\n",
    "        pyautogui.press('tab')\n",
    "        pyautogui.press('tab')\n",
    "        pyautogui.press('tab')\n",
    "        \n",
    "        for i in range(vpn_switch):\n",
    "            pyautogui.press('down')\n",
    "\n",
    "        pyautogui.press('enter')\n",
    "        time.sleep(longdelay)\n",
    "        #pyautogui.getWindowsWithTitle('MiCOURT Case Search')[0].maximize()\n",
    "        pyautogui.getWindowsWithTitle('MiCOURT Case Search')[0].activate()\n",
    "    '''\n",
    "    if ntries > maxretries:\n",
    "        #break\n",
    "        time.sleep(25)\n",
    "        continue\n",
    "        \n",
    "    time.sleep(20)\n",
    "    \n",
    "time.sleep(shortdelay)\n",
    "if ntries > maxretries:\n",
    "    pyautogui.alert(title=\"Problem!\", text=\"Max retries reached; please review last few downloads.\")\n",
    "else:\n",
    "    pyautogui.alert(title=\"Done!\", text=\"Done!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc-autonumbering": false,
  "toc-showmarkdowntxt": true,
  "toc-showtags": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
